package com.warframe.invasion;

import com.alibaba.fastjson.JSONArray;
import com.tools.HibernateUtils;
import com.tools.StringContent;
import com.warframe.entity.database.WikiTranslateEO;
import com.warframe.entity.invasions.InvasionMO;
import com.warframe.entity.message.Message;
import com.warframe.message.Executor;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.hibernate.SQLQuery;
import org.hibernate.Session;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Invasion implements Runnable {

    private static String url = "https://api.warframestat.us/pc/invasions";
    private static String sql = "select * from WikiTranslate a where a.item_en in (TARGET) order by a.item_cn";
    private Message message;
    private List<InvasionMO> invasionMOList;
    private List<String> stringList;
    private List<String> params;

    public Invasion(Message message) {
        this.message = message;
        invasionMOList = new ArrayList<>();
        stringList = new ArrayList<>();
        params = new ArrayList<>();
    }

    private void getResult() {
        CloseableHttpClient httpClient = HttpClients.createDefault(); // 创建httpClient实例
        HttpGet httpGet = new HttpGet(url); // 创建httpget实例
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36");
        CloseableHttpResponse response = null; // 执行http get请求
        try {
            response = httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity entity = response.getEntity(); // 获取返回实体
        String res = null;
        try {
            res = EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }
        invasionMOList = JSONArray.parseArray(res, InvasionMO.class);
        List<InvasionMO> currentInvasionList = new ArrayList<>();
        for (InvasionMO eachMO : invasionMOList) {
            if (eachMO.getCompletion() >= 0 && eachMO.getCompletion() <= 100) {
                currentInvasionList.add(eachMO);
            }
        }
        invasionMOList = currentInvasionList;
    }

    private void format() {
        for (InvasionMO eachMO : invasionMOList) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(eachMO.getNode() + "进度 : " + eachMO.getCompletion().toString() + "\n");
            stringBuilder.append("进攻奖励 : ");
            if (eachMO.getAttackerReward().getCountedItems().size() == 0) {
                stringBuilder.append("无 \n");
            } else {
                stringBuilder.append(eachMO.getAttackerReward().getCountedItems().get(0).getType() + "\n");
                params.add(eachMO.getAttackerReward().getCountedItems().get(0).getType());
            }
            stringBuilder.append("防守奖励 : ");
            if (eachMO.getDefenderReward().getCountedItems().size() == 0) {
                stringBuilder.append("无 \n");
            } else {
                stringBuilder.append(eachMO.getDefenderReward().getCountedItems().get(0).getType() + "\n");
                params.add(eachMO.getDefenderReward().getCountedItems().get(0).getType());
            }
            stringList.add(stringBuilder.toString());
            String node = eachMO.getNode();
            params.add(node.substring(node.indexOf("(") + 1, node.lastIndexOf(")")));
        }
    }

    private void explain() {
        StringBuilder target = new StringBuilder();
        for (String eachStr : params) {
            target.append("'" + eachStr + "'");
        }
        String str = target.toString();
        str = str.replace("''", "','");
        Session session = HibernateUtils.getSessionFactory().openSession();
        SQLQuery sqlQuery = session.createSQLQuery(sql.replace("TARGET", str));
        List<WikiTranslateEO> wikiTranslateEOList = sqlQuery.addEntity(WikiTranslateEO.class).list();
        List<String> resultList = new ArrayList<>();
        for (String eachStr : stringList) {
            for (WikiTranslateEO eachEO : wikiTranslateEOList) {
                eachStr = eachStr.replace(eachEO.getItemEn(), eachEO.getItemCn());
            }
            resultList.add(eachStr);
        }
        stringList = resultList;
    }

    @Override
    public void run() {
        getResult();
        format();
        explain();
        StringBuilder msg = new StringBuilder();
        Integer count = 0;
        for (String eachStr : stringList) {
            msg.append(eachStr);
            count++;
            if (count == 4) {
                Message newMessage = new Message();
                newMessage.setSign(StringContent.SENDPERSON);
                newMessage.setMsg(msg.toString());
                newMessage.setFromQQ(message.getFromQQ());
                Executor.getInstance().pushMsg(newMessage);
                msg.delete(0, msg.length());
                count = 0;
            }
        }
        Message newMessage = new Message();
        newMessage.setSign(StringContent.SENDPERSON);
        newMessage.setMsg(msg.toString());
        newMessage.setFromQQ(message.getFromQQ());
        Executor.getInstance().pushMsg(newMessage);
    }
}
